package Dao;

import domain.Student;

public class StudentDao {
    static Student[] stus = new Student[5];
    static{
        Student stu1 = new Student("001","zs","23","1999");
        stus[0] = stu1;
    }


    public boolean add(Student stu) {
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if (student == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;
        } else {
            stus[index] = stu;
            return true;
        }
    }

    public Student[] findAll() {

        return stus;
    }

    public void delete(String delId) {

        int index = getIndex(delId);
        stus[index] = null;
    }

    public int getIndex(String id) {

        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu != null && stu.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void update(String updateId, Student newStu) {

        int index = getIndex(updateId);
        stus[index] = newStu;
    }
}
